package com.haiyou.common.interceptor;

import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Enumeration;
import java.util.Map;

/**
 * @author: chenzeke
 * @date: 2018/11/22 16:24
 * @description: 入口拦截器
 */
@Slf4j
@Component
public class EntranceInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object object) {
        JSONObject header = new JSONObject();
        Enumeration names = request.getHeaderNames();
        while (names.hasMoreElements()) {
            String name = (String) names.nextElement();
            header.put(name, request.getHeader(name));
        }
        log.info("header===>" + header);
        JSONObject data = new JSONObject();
        Map<String, String[]> parameterMap = request.getParameterMap();
        data.put("parameterMap", parameterMap);
        data.put("url", request.getRequestURI());
        data.put("method", request.getMethod());
        data.put("name", object.getClass().getSimpleName());
        log.info("data===>" + data);
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object object, ModelAndView modelAndView) {

    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object o, Exception e) {

    }
}